package com.example.administrator.daydayappmarket.software.choice.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.recommend.activity.RecommendTopImageSpecialThirdActivity;
import com.example.administrator.daydayappmarket.software.choice.model.SoftwareChoiceAppsItem;
import com.example.administrator.daydayappmarket.software.choice.model.SoftwareChoiceParent;
import com.example.administrator.daydayappmarket.software.choice.model.SoftwareChoicePicTopic;
import com.example.administrator.daydayappmarket.software.choice.model.SoftwareChoiceTopicApps;
import com.example.administrator.daydayappmarket.software.choice.model.SoftwareChoiceTopicItem;
import com.example.administrator.daydayappmarket.software.utils.MyGridView;
import com.example.administrator.daydayappmarket.software.utils.MyListView;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by lenovo on 2016/12/26.
 */

public class SoftwareChoiceAdapter extends BaseAdapter  {
    private Context context;
    private List<SoftwareChoiceParent> data;

    public SoftwareChoiceAdapter(Context context, List<SoftwareChoiceParent> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public SoftwareChoiceParent getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {

        int type = 0;
        SoftwareChoiceParent item = getItem(position);
        String theme_type = item.getTheme_type();

        if (theme_type.equals("app_topic")) {
            type = 0;//证明布局是GridView  是装机必备
        }
        if (theme_type.equals("apps")) {
            type = 1;//证明布局是ListView
        }
        if (theme_type.equals("pic_topic")) {
            type = 2;//是刷新后出来的第一个视图
        }


        return type;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        int itemViewType = getItemViewType(position);

        ViewHolder viewHolder = null;
        ViewHolderSecond viewHolderSecond = null;
        ViewHolderRefresh viewHolderRefresh=null;

        if (convertView == null) {
            switch (itemViewType) {
                case 0:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_software_choice_firstlayout, viewGroup, false);
                    viewHolder = new ViewHolder(convertView);
                    convertView.setTag(viewHolder);
                    break;
                case 1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_software_choice_secondlayout, viewGroup, false);
                    viewHolderSecond = new ViewHolderSecond(convertView);
                    convertView.setTag(viewHolderSecond);
                    break;
                case 2:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_software_choice_refresh, viewGroup, false);
                    viewHolderRefresh=new ViewHolderRefresh(convertView);
                    convertView.setTag(viewHolderRefresh);
                    break;
            }

        } else {
            switch (itemViewType) {
                case 0:
                    viewHolder = (ViewHolder) convertView.getTag();
                    break;
                case 1:
                    viewHolderSecond = (ViewHolderSecond) convertView.getTag();
                    break;
                case 2:
                    viewHolderRefresh= (ViewHolderRefresh) convertView.getTag();
                    break;
            }
        }

        SoftwareChoiceParent item = getItem(position);

        switch (itemViewType) {
            case 0:
                final SoftwareChoiceTopicItem softwareChoiceTopicItem = (SoftwareChoiceTopicItem) item;
                viewHolder.itemSoftwareChoiceTextView1.setText(softwareChoiceTopicItem.getTopic_title());
                viewHolder.itemSoftwareChoiceTextView2.setText(softwareChoiceTopicItem.getTopic_name());
                viewHolder.itemSoftwareChoiceTextView1.setBackgroundColor(Color.parseColor(softwareChoiceTopicItem.getTitle_color()));
                List<SoftwareChoiceTopicApps> apps = softwareChoiceTopicItem.getApps();
                SoftwareChoiceGridViewAdapter adapter = new SoftwareChoiceGridViewAdapter(context, apps);
                viewHolder.itemSoftwareChoiceGridView.setAdapter(adapter);


                break;
            case 1:
                final SoftwareChoiceAppsItem softwareChoiceAppsItem = (SoftwareChoiceAppsItem) item;
                List<SoftwareChoiceTopicApps> softwareChoiceTopicAppses = softwareChoiceAppsItem.getSoftwareChoiceTopicAppses();
                SoftwareChoiceListViewAdapter adapter1 = new SoftwareChoiceListViewAdapter(context, softwareChoiceTopicAppses);
                viewHolderSecond.itemSoftwareChoiceListView.setAdapter(adapter1);


                break;
            case 2:
                final SoftwareChoicePicTopic softwareChoicePicTopic= (SoftwareChoicePicTopic) item;
                viewHolderRefresh.itemSoftwareChoiceRefreshTextView1.setText(softwareChoicePicTopic.getTopic_title());
                viewHolderRefresh.itemSoftwareChoiceRefreshTextView2.setText(softwareChoicePicTopic.getTopic_name());
                viewHolderRefresh.itemSoftwareChoiceRefreshImageView.setImageResource(R.mipmap.app_default);
                if(softwareChoicePicTopic.getImage().startsWith(Contacts.FRONT_IMG)){
                    Picasso.with(context).load(softwareChoicePicTopic.getImage()).into(viewHolderRefresh.itemSoftwareChoiceRefreshImageView);
                }else{
                    Picasso.with(context).load(Contacts.FRONT_IMG+softwareChoicePicTopic.getImage()).into(viewHolderRefresh.itemSoftwareChoiceRefreshImageView);

                }
                viewHolderRefresh.itemSoftwareChoiceRefreshTextView1.setBackgroundColor(Color.parseColor(softwareChoicePicTopic.getTitle_color()));
                viewHolderRefresh.itemSoftwareChoiceRefreshImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String id = softwareChoicePicTopic.getId();
                        Intent intent=new Intent(context, RecommendTopImageSpecialThirdActivity.class);
                        intent.putExtra("id",id);
                        context.startActivity(intent);
                    }
                });

                break;
        }


        return convertView;
    }


    static class ViewHolder {
        @InjectView(R.id.item_software_choice_textView1)
        TextView itemSoftwareChoiceTextView1;
        @InjectView(R.id.item_software_choice_textView2)
        TextView itemSoftwareChoiceTextView2;
        @InjectView(R.id.item_software_choice_gridView)
        MyGridView itemSoftwareChoiceGridView;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

    static class ViewHolderSecond {
        @InjectView(R.id.item_software_choice_listView)
        MyListView itemSoftwareChoiceListView;

        ViewHolderSecond(View view) {
            ButterKnife.inject(this, view);
        }
    }

    static class ViewHolderRefresh {
        @InjectView(R.id.item_software_choice_refresh_textView1)
        TextView itemSoftwareChoiceRefreshTextView1;
        @InjectView(R.id.item_software_choice_refresh_textView2)
        TextView itemSoftwareChoiceRefreshTextView2;
        @InjectView(R.id.item_software_choice_refresh_imageView)
        ImageView itemSoftwareChoiceRefreshImageView;

        ViewHolderRefresh(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
